import 'package:intl/intl.dart';
import 'package:tiktokadult/hj_utils/global.dart';

extension DateString on String? {
  String? utcTimeSToYMDHMSString() {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    const format = 'yyyy-MM-dd HH:mm:ss';
    final dateF = DateFormat(format);
    return dateF.format(date);
  }

  String? utcTimeSToYMDHMString({String gap = "-"}) {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    String format = "yyyy${gap}MM${gap}dd HH:mm";
    final dateF = DateFormat(format);
    return dateF.format(date);
  }

  /// yyyy年MM月dd日
  String? utcTimeToChineseYMDString() {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    return '${date.year}年${date.month}月${date.day}日';
  }

  /// mm月 dd日
  String? utcTimeToChineseMDString() {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    return '${date.month}月${date.day}日';
  }

  /// yyyy-MM-dd
  String? utcTimeSToYMDString({String gap = "-"}) {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    String format = "yyyy${gap}MM${gap}dd";
    final dateF = DateFormat(format);
    return dateF.format(date);
  }

  /// yyyy-MM-dd
  String? utcTimeSToMDHMString({String gap = "-"}) {
    if (this == null || (this?.isEmpty ?? true)) return '';
    DateTime date = DateTime.parse(this!).toLocal();
    String format = "MM${gap}dd HH:mm";
    final dateF = DateFormat(format);
    return dateF.format(date);
  }

  String? utcYMDHM({String gap = "-"}) {
    if (this?.isNotEmpty != true) {
      return "近期";
    }
    var now = DateTime.now();
    var date = DateTime.tryParse(this!)?.toLocal();
    if (date == null) return "";
    final changeTime = now.difference(date); //秒

    int s = changeTime.inSeconds;
    if (s >= 0 && s < 60) {
      return "刚刚";
    }
    final int m = changeTime.inMinutes;
    if (m > 0 && m < 60) {
      return "${m + 1}分钟前";
    }
    final int h = changeTime.inHours;
    if (h > 0 && h < 24) {
      return "${h + 1}小时前";
    }
    final int d = changeTime.inDays;
    if (d > 0 && d < 3) {
      return "${d + 1}天前";
    }
    String timeNow = date.ymdSMFormat(gap: gap); //2018年09月16日 23时16分15秒
    return timeNow;
  }
}
